import datetime
from datetime import timedelta

from django.utils import timezone
from django.test import TestCase
from django.urls import reverse

from .models import Question


class QuestionModelTests(TestCase):

    def test_was_published_recently_with_future_question(self):
        """
        was_published_recently() returns False for questions whose pub_date
        is in the future.
        """
        time = timezone.now() + timedelta(hours=3)
        future_question = Question(pub_date=time)
        self.assertIs(future_question.was_published_recently(), False)


def create_question(question_text, days):
    time = timezone.now() + datetime.timedelta(days=days)
    return Question.objects.create(question_text=question_text, pub_date=time)


class QuestionIndexViewTests(TestCase):

    def test_no_questions(self):
        """当不存在任何问题时，应有适当的信息显示出来"""
        r = self.client.get(reverse('polls:index'))
        self.assertEqual(r.status_code, 200)
        self.assertContains(r, "No polls are available.")
        self.assertQuerysetEqual(r.context['latest_questions'], [])

    def test_past_question(self):
        """pub_date为过去时间的问题显示在index页面"""
        create_question(question_text="Past question.", days=-30)
        r = self.client.get(reverse('polls:index'))
        self.assertQuerysetEqual(
            r.context['latest_questions'],
            ['<Question: Past question.>']
        )

    def test_future_question(self):
        """发布时间为将来时间的问题不显示在index页面上"""
        create_question(question_text="Future question.", days=30)
        r = self.client.get(reverse('polls:index'))
        self.assertQuerysetEqual(r.context['latest_questions'], [])

    def test_future_question_and_past_question(self):
        """时间为将来和过去的两个问题同时存在，仅仅显示过去的问题"""
        create_question(question_text="Past question.", days=-30)
        create_question(question_text="Future question.", days=30)
        r = self.client.get(reverse('polls:index'))
        self.assertQuerysetEqual(
            r.context['latest_questions'],
            ['<Question: Past question.>']
        )

    def test_two_past_questions(self):
        """index页面可以显示多个问题"""
        create_question(question_text="Past question 1.", days=-30)
        create_question(question_text="Past question 2.", days=-5)
        r = self.client.get(reverse('polls:index'))
        self.assertEqual(len(r.context['latest_questions']), 2)


class QuestionDetailViewTests(TestCase):
    def test_future_question(self):
        """发布时间为将来的问题，访问其detail页面将返回404"""
        future_question = create_question(question_text='Future question.', days=5)
        url = reverse('polls:detail', args=(future_question.id,))
        r = self.client.get(url)
        self.assertEqual(r.status_code, 404)

    def test_past_question(self):
        """发布时间为过去的问题，访问其detail页面将得到问题的文本"""
        past_question = create_question(question_text='Past Question.', days=-5)
        url = reverse('polls:detail', args=(past_question.id,))
        r = self.client.get(url)
        self.assertContains(r, past_question.question_text)
